#include <stdio.h>
#include "Broker.h"
#include "Publisher.h"
#include "Subscriber.h"
#include "fsk.msg.messages.pb.h"

class Msg01Reader : public Subscriber::DataReaderTemplate<fsk::msg::Message01>
{

  private:
    unsigned val_;

    void handle(fsk::msg::Message01& msg)
    {
      printf("(%s:%d) handling msg 01 -- %d\n",__FILE__,__LINE__, msg.val01());
      val_=msg.val01();
    }

  public:
    Msg01Reader() { }
    unsigned val() { return val_; }
};

void test00()
{
  Publisher pub;
  fsk::msg::Message01 msg;
  Subscriber sub;
  Msg01Reader dr;
  sub.registerHandler(msg,dr);
  while(1)
  {
    ::usleep(30000000);
  }
}

void test01()
{
  Publisher pub;
  fsk::msg::Message01 msg;
  Subscriber sub;
  Msg01Reader dr;
  sub.registerHandler(msg,dr);
  ::usleep(5000);
  const int NumIterations = 10;

  for(int i=0; i<NumIterations; ++i)
  {
    msg.set_val01(i);
    pub.publish(msg);
    ::usleep(5000);
//  assert(dr.val()==i);
  }
}

int main(int argc, char* argv[])
{
  printf("(%s:%d) main process initializing\n",__FILE__,__LINE__);
  Broker broker;
  broker.start();
  test00();
//test01();
  printf("(%s:%d) main process terminating\n",__FILE__,__LINE__);
}
